//
//  MLAMineVC.m
//  MagicLampApp
//
//  Created by Zheng on 2018/12/18.
//  Copyright © 2018 zheng.magic.lamp.app. All rights reserved.
//

#import "MLAMineVC.h"
#import "MLAFundsVC.h"
#import "MLAManageVC.h"
#import "MLAPropertyVC.h"
#import "MLASavePinVC.h"
#import "MLASettingsVC.h"
#import "MLAMessageVC.h"
#import "MLAMyFoodVC.h"

@interface MLAMineVC ()

@end

@implementation MLAMineVC {
    UILabel *nameLbl;
    UIImageView *iconImgv;
}

- (void)viewDidLoad {
    [super viewDidLoad];

//    self.titlesArr = @[@"我的费用",@"我的经营",@"我的物业",@"我的餐饮",@"我的进销存"];
    
    self.titlesArr = @[@"我的费用",@"我的经营",@"我的物业",@"我的进销存"];

    [self.tableView reloadData];

    [self addSubviews];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self loadData];
}

#pragma mark - LoadData
- (void)loadData {
    
    [MLAHttpManage postHostType:MLAHttpHostType_Api api:kApiUserInfo params:nil success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        self->nameLbl.text = response[@"username"];
        [self->iconImgv sd_setImageWithURL:[NSURL URLWithString:response[@"pic"]]];
    } failure:^{
        
    }];
}

#pragma mark - Actions
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [super tableView:tableView didSelectRowAtIndexPath:indexPath];
    if (indexPath.row == 0) {
        CX_Log(@"我的费用");
        MLAFundsVC *fundsVC = [[MLAFundsVC alloc] init];
        [self.navigationController pushViewController:fundsVC animated:YES];
    } else if (indexPath.row == 1) {
        CX_Log(@"我的经营");
        MLAManageVC *manageVC = [[MLAManageVC alloc] init];
        [self.navigationController pushViewController:manageVC animated:YES];
    } else if (indexPath.row == 2) {
        CX_Log(@"我的物业");
        MLAPropertyVC *propertyVC = [[MLAPropertyVC alloc] init];
        [self.navigationController pushViewController:propertyVC animated:YES];
    } else if (indexPath.row == 3) {
        CX_Log(@"我的餐饮");
//        MLAFoodVC *fdVC = [[MLAFoodVC alloc] init];
//        NSString *filePath = [NSString stringWithFormat:@"file://%@/index.js",[NSBundle mainBundle].bundlePath];
//        fdVC.url = [NSURL URLWithString:filePath];
//        [self.navigationController pushViewController:fdVC animated:YES];
        MLASavePinVC *savePinVC = [[MLASavePinVC alloc] init];
        [self.navigationController pushViewController:savePinVC animated:YES];

    } else if (indexPath.row == 4) {
        CX_Log(@"我的进销存");
        MLASavePinVC *savePinVC = [[MLASavePinVC alloc] init];
        [self.navigationController pushViewController:savePinVC animated:YES];
    }
}

- (void)leftItemAction {
    CX_Log(@"设置");
    MLASettingsVC *settingsVC = [[MLASettingsVC alloc] init];
    [self.navigationController pushViewController:settingsVC animated:YES];
}

- (void)rightItemAction {
    CX_Log(@"消息");
    MLAMessageVC *messageVC = [[MLAMessageVC alloc] init];
    [self.navigationController pushViewController:messageVC animated:YES];
}

#pragma mark - Subviews
- (void)addSubviews {
    
    [self configLeftItemWithImage:@"mine_set"];
    [self configRightItemWithImage:@"mine_msg"];
    
    UIView *tableHeaderView = [[UIView alloc] init];
    tableHeaderView.frame = CGRectMake(0, 0, CX_GetScreenWidth(), 128);
    
    nameLbl = [[UILabel alloc] init];
    nameLbl.textColor = CX_GetColorTitle();
    nameLbl.font = CX_GetBoldFont(26);
    [tableHeaderView addSubview:nameLbl];
    [nameLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(35);
    }];
    
    iconImgv = [[UIImageView alloc] init];
    iconImgv.layer.cornerRadius = 37.5;
    iconImgv.layer.masksToBounds = YES;
    [tableHeaderView addSubview:iconImgv];
    [iconImgv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(75);
        make.centerY.equalTo(self->nameLbl);
        make.right.mas_equalTo(-15);
    }];
    
    self.tableView.tableHeaderView = tableHeaderView;

    /*
     测试数据
     nameLbl.text = @"Leslie Kng";
     iconImgv.backgroundColor = CX_GetColorRandom();
     */
}

@end
